#include "stdafx.h"
#include "EditorMouseHandler.h"
#include "terrain.h"
#include "MapManager.h"
#include "inputmanager.h"
#include "gamemanager.h"
#include "Entity.h"
#include "ObjectManager.h"
#include "ForceEntity.h"
#include "Kingdom.h"

SEditorMouseHandler *g_pEditorMouseHandler = NULL;
STerrainHandler g_terrainhandler;
SEntityHandler g_entityhandler;

void SEditorMouseHandler::SetCurHandler( SEditorMouseHandler *handler )
{
    if( g_pEditorMouseHandler )
        g_pEditorMouseHandler->Cancel();
    g_pEditorMouseHandler = handler;
}


void STerrainHandler::Update()
{
    SetBack();
    GetTerrain();
}

void STerrainHandler::SetBack()
{
    for( uint32 i=0;i<m_grids.size(); i++ )
    {
        SGrid &g = m_grids[i];
        SGridData gd;
        gd.gridType = g.type;
        CTerrain::Instance().SetGridTerrain( g.x, g.y, gd );
    }
    m_grids.clear();
}

void STerrainHandler::GetTerrain()
{
    SEditorMouseHandler::Update();
    int x = (mousepos.x-gc.mapregion.x1)/gc.gridsize;
    int y = (mousepos.y-gc.mapregion.y1)/gc.gridsize;
    for( int i=x;i<x+m_size;i++ )
    {
        for( int j=y; j<y+m_size; j++ )
        {
            SGridData *griddata = CTerrain::Instance().GetGrid( i, j );
            if( griddata == NULL )
                continue;
            SGrid g = {i, j, griddata->gridType};
            m_grids.push_back( g );

            griddata->gridType = m_terrain;
            // g_log.WriteInfo("Set %d, %d to %d", i, j, m_terrain);
        }
    }
}

STerrainHandler::STerrainHandler()
{
   
}

void STerrainHandler::Do()
{
    m_grids.clear();
}

void STerrainHandler::Undo()
{

}

void STerrainHandler::Redo()
{

}

void STerrainHandler::Cancel()
{
    SetBack();
}

void SEntityHandler::SetEntity( sgEntity *e )
{
    Cancel();
    CObjectManager::Instance().AddObject( e );
    curEntity = e;
}

SEntityHandler::SEntityHandler()
{
    curEntity = NULL;
}

void SEntityHandler::Update()
{
    SEditorMouseHandler::Update();
    if( curEntity )
    {
        curEntity->SetPosition( mousepos );
    }
}

void SEntityHandler::Do()
{
    if(curEntity)
    {
        sgEntity *e = curEntity->Clone();
        CObjectManager::Instance().AddObject( e );
        sgForceEntity *fe = (sgForceEntity *)e;
        sgKingdom *kd = fe->GetOwner();
        if( kd )
        {
            if( entityType == army )
            {
                fe->SetOwner(kd);
                kd->AddArmy( (sgArmy*)fe );
            }
            if( entityType == city )
            {
                fe->SetOwner(kd);
                kd->AddCity( (sgCity*)fe );
            }
        }
    }
}

void SEntityHandler::Undo()
{

}

void SEntityHandler::Redo()
{

}

void SEntityHandler::Cancel()
{
    CObjectManager::Instance().RemoveObject( curEntity );
    SAFE_DELETE(curEntity);
}

void SEditorMouseHandler::Update()
{
    const InputInfo &info = CInputManager::Instance().GetInputInfo();
    POINT p = CInputManager::Instance().GetMousePos( );
    p.x = p.x + gc.screenrect.x1;
    p.y = p.y + gc.screenrect.y1;
    IRect r(p.x, p.y, p.x, p.y );
    CMapManager::Instance().ConvertToOriginalCoord( r );
    mousepos.x = r.x1;
    mousepos.y = r.y1;
}
